package com.example.user.myfirstapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.user.myfirstapp.MyApplication;
import com.example.user.myfirstapp.R;
import com.example.user.myfirstapp.common.Constant;
import com.example.user.myfirstapp.common.LoadingDialog;
import com.example.user.myfirstapp.common.StringHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by user on 2016/5/20.
 */
public class RegisterActivity extends Activity  implements View.OnClickListener {
    private int registResultCode = 2;
    private int backCode = 1;
    private EditText et_register_username,et_register_pwd,et_register_pwd_again;
    private TextView tv_cancel;
    private Button btn_register;
    private Intent intent;
    private String userName,pwd,pwd_again,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        et_register_username=(EditText) findViewById(R.id.et_register_username);
        et_register_pwd=(EditText) findViewById(R.id.et_register_pwd);
        et_register_pwd_again=(EditText) findViewById(R.id.et_register_pwd_again);
        btn_register=(Button)findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this);
        tv_cancel=(TextView)findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                register();
                break;
            case R.id.tv_cancel:
                onBackPressed();
            default:
                break;
        }
    }

    //注册
    public void register(){
        try {
            userName = et_register_username.getText().toString();
            pwd = et_register_pwd.getText().toString();
            pwd_again = et_register_pwd_again.getText().toString();
            if (TextUtils.isEmpty(userName)) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(pwd)) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(pwd_again)) {
                Toast.makeText(this, "请输入确认密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!pwd.equals(pwd_again)) {
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            //显示加载中
            LoadingDialog.showLoading(this,"请稍后...");
            //插入数据到服务器.
            password = StringHelper.md5EncryptString(pwd);
            String url = "http://"+ Constant.host+":"+Constant.port+"/api/UserInfo/Insert";
            StringRequest request = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String result) {
                            if(result.equals("true")){
                                //传值到登陆页
                                gotoLogin();
                                //关闭提示语
                                LoadingDialog.closeLoading();
                                //关闭当前activity
                                finish();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError err) {
                            Toast.makeText(getApplicationContext(), "网络请求失败",
                                    Toast.LENGTH_LONG).show();
                            //关闭提示语
                            LoadingDialog.closeLoading();
                        }
                    }
            ){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> params = new HashMap<>() ;
                    params.put("userName",userName) ;
                    params.put("password",password) ;
                    return params ;
                }
            };
            request.setTag("registerRequest");
            MyApplication.getHttpQueue().add(request);
        }
        catch (Exception e)
        {
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
            //关闭提示语
            LoadingDialog.closeLoading();
        }
    }

    private void gotoLogin(){
        intent = new Intent();
        Bundle bun = new Bundle();
        bun.putString("username", userName);
        bun.putString("userpwd", pwd);//返回的密码不用加密
        intent.setClass(this, LoginActivity.class);
        intent.putExtras(bun);
        this.setResult(registResultCode, intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //结束时队列里面注销
        MyApplication.getHttpQueue().cancelAll("registerRequest");
    }

    /*
    * 重写onBackPressed()，不然直接按手机返回键会报错
    */
    @Override
    public void onBackPressed() {
        intent = new Intent();
        this.setResult(backCode, intent);
        this.finish();
    }
}
